package org.erepublik.company.controllers;

import java.util.ArrayList;

import org.erepublik.company.Job;
import org.erepublik.company.log.SitesLogger;
import org.erepublik.company.product.Food;
import org.erepublik.company.searchCriteria.EAllianceSearchCriteria;
import org.erepublik.company.searchCriteria.JobSearchCriteria;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/jobs.html")
public class JobController extends AbstractController {

    @Autowired // autumatycznie wstrzykiwany obiekt
    private SitesLogger sitesLogger; 

    @Autowired // autumatycznie wstrzykiwany obiekt
    private IProductsService productService; 
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
        JobSearchCriteria criteria = new JobSearchCriteria();
        Job[] products = productService.getJobs(null, 10);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceJobUpdate() );
        model.addAttribute("jobs", products);
        model.addAttribute("jobSearchCriteria",criteria);
        sitesLogger.log("JOBS", "Country: null Skill: null Minimum skill: 10 Time: 1 hour");
        return "jobs";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("jobSearchCriteria") JobSearchCriteria criteria) { 
        fillModelMap(model);
        Job[] products = productService.getJobs(criteria.getCountry(), criteria.getMinimalSkill().getValue());
        products = filter(products, criteria.getAlliance());
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceJobUpdate());
        model.addAttribute("jobs", products);
        sitesLogger.log("JOBS", "Country: "+criteria.getCountry()+
  
                " Minimum skill: "+criteria.getMinimalSkill());
        return "jobs";
    }
    
    
    
    private Job[] filter(Job[] products, EAllianceSearchCriteria alliance) {
        ArrayList<Job> list = new ArrayList<Job>();
        for (Job food : products) {
            if ( Utils.contains( food.getCountry().getAlliance(), alliance.getAlliances() ) )
                list.add(food);
        }
        return list.toArray(new Job[0]);
    
    }



    final int MAX_SIZE=50; // maksymalna ilosc wyswietlanych produktow
    // skraca tabele do MAX_SIZE produktow
    private Job[] shrinkTable(Job[] products) {
        if (products.length <= MAX_SIZE)
            return products;
        else {
            Job[] newTable = new Job[MAX_SIZE];
            for (int i=0; i < MAX_SIZE; i++) {
                newTable[i]=products[i];
            }
            return newTable;
        }
    }
    
    
}
